﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HZWebService;
using System.IO;

namespace WebConfigManager.TabPageControls
{
    public partial class PageConfigHzService : PageConfigBase
    {

        private HzClientOption _option = new HzClientOption();
        public HzClientOption Option
        {
            get { return _option; }
        }

        public override bool LoadConfig()
        {
            if (!File.Exists(_option.ConfigFilePath))
            {
                _option.Save();
            }
            else
            {
                _option.Load();
            }
            return true;
        }

        public PageConfigHzService()
        {
            InitializeComponent();
        }

        private bool CheckInputting()
        {
            // 检查填写是否正确
            if (string.IsNullOrEmpty(textBoxUserName.Text))
            {
                MessageBox.Show("账号名称为空.");
                return false;
            }
            if (string.IsNullOrEmpty(textBoxPassWord.Text))
            {
                MessageBox.Show("密码不能为空.");
                return false;
            }

            if (string.IsNullOrEmpty(textBoxURL.Text))
            {
                MessageBox.Show("接口服务地址不能为空.");
                return false;
            }

            return true;
        }



        public override bool InitCtrls()
        {

            metroToggleEnabled.Checked = _option.Enabled;

            textBoxUserName.Text = _option.User;
            textBoxPassWord.Text = _option.GetPassword();
            textBoxURL.Text = _option.URL;

            return true; 
        }

        public override bool UpdateData()
        {
            if (!CheckInputting())
            {
                return false;
            }
            _option.Enabled = metroToggleEnabled.Checked;
            _option.URL = textBoxURL.Text;
            _option.User = textBoxUserName.Text;

            _option.MakeCiphertext(textBoxPassWord.Text);

            _option.Save();
            return true;
        }

    }
}
